package com.cy.oa.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cy.oa.service.ActService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
@RequestMapping("/deployRep")
public class DeployRepositoryController {

	@Resource
	RepositoryService repositoryService;
	@Resource
	ActService actService;
	@Resource
	RuntimeService runtimeService;
	
	@RequestMapping("/test")
	public List<Map<String,Object>> test() {
		List<Deployment> resList= repositoryService.createDeploymentQuery().orderByDeploymenTime()
				                             .asc().list();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		for(Deployment deployment : resList) {
			HashMap<String,Object> deploymentMap = new HashMap<String,Object>();
			deploymentMap.put("ID", deployment.getId());
			deploymentMap.put("Category", deployment.getCategory());
			deploymentMap.put("Name", deployment.getName());
			deploymentMap.put("TenantId", deployment.getTenantId());
			deploymentMap.put("DeploymentTime", sdf.format(deployment.getDeploymentTime()));
			list.add(deploymentMap);
		}
		return list;
	}
	
	@RequestMapping("/leaveProcessImg")
	public ModelAndView leaveProcessImg(String deploymentId,ModelMap model) {
		System.out.println("deploymentId:"+deploymentId);
		ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
				            .deploymentId(deploymentId).singleResult();
		System.out.println("processDefinition:"+processDefinition);
		String imgName = processDefinition.getDiagramResourceName();
		model.addAttribute("deploymentId",deploymentId);
		model.addAttribute("imgName",imgName);
		return new ModelAndView("oa/leave/leaveProcessImage",model);
	}
	
	@RequestMapping("/leaveProcessImg2")
	public ModelAndView leaveProcessImg2(String processInstanceId,ModelMap modelMap) {
		System.out.println("processInstanceId:"+processInstanceId);
		ProcessDefinition processDefinition = actService.getProcessDefinitionByInstanceId(processInstanceId);
		System.out.println("processDefinition:"+processDefinition);
		String imgName = processDefinition.getDiagramResourceName();
		String deploymentId = processDefinition.getDeploymentId();
		//获取流程定义实体
		ProcessDefinitionEntity pEntity = (ProcessDefinitionEntity) processDefinition;
		//获取流程实例
		ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
				.processInstanceId(processInstanceId).singleResult();
		//流程如果结束，processIntance为空
		ActivityImpl activityImpl =null;
		if(null!=processInstance) {
			String activityId = processInstance.getActivityId();
			activityImpl = pEntity.findActivity(activityId);
		}
		
		modelMap.addAttribute("deploymentId",deploymentId);
		modelMap.addAttribute("imgName",imgName);
		modelMap.addAttribute("activityImpl",activityImpl);
		return new ModelAndView("oa/leave/leaveProcessImage2",modelMap);
	}
	
	@RequestMapping("/leaveImg")
	public void leaveImg(String deploymentId,String imgName,HttpServletResponse response) {
		InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, imgName);
		response.setCharacterEncoding("utf-8");
		response.setContentType("img/png");
		try {
			OutputStream out = response.getOutputStream();
			int length = 0;
			byte[] buffer = new byte[1024];
			while((length=inputStream.read(buffer))!=-1) {
				out.write(buffer,0,length);
			}
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	@RequestMapping("/leaveProcessImg2Re")
	public void leaveProcessImg(String deploymentId,String imgName,HttpServletResponse response) {
		InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, imgName);
		response.setCharacterEncoding("utf-8");
		response.setContentType("img/png");
		try {
			OutputStream out = response.getOutputStream();
			int length = 0;
			byte[] buffer = new byte[1024];
			while((length=inputStream.read(buffer))!=-1) {
				out.write(buffer,0,length);
			}
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
}
